<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img2.baidu.com/it/u=777661950,740942389&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >python模块调用</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h1 id="模块"><a href="#模块" class="headerlink" title="模块"></a>模块</h1><h2 id="一、-模块-what-why-how"><a href="#一、-模块-what-why-how" class="headerlink" title="一、 模块(what  why  how)"></a>一、 模块(what  why  how)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是模块</span><br><span class="line">	包含一系列功能的一个python文件</span><br><span class="line">	内置模块</span><br><span class="line">	第三方模块</span><br><span class="line">	自定义模块</span><br><span class="line"><span class="comment"># 模块有四种形式</span></span><br><span class="line">	I 使用python编写的.py文件 ******</span><br><span class="line"></span><br><span class="line">　　 II 已被编译为共享库或DLL的C或C++扩展</span><br><span class="line"></span><br><span class="line">　　 III 把一系列模块组织到一起的文件夹（注：文件夹下有一个__init__.py文件，该文件夹称之为包）*******</span><br><span class="line"></span><br><span class="line">　　 IV 使用C编写并链接到python解释器的内置模块</span><br><span class="line"><span class="number">2.</span>为什么要用模块</span><br><span class="line">	因为用模块后代码就不会冗余，而且代码结构清晰</span><br><span class="line"><span class="number">3.</span>怎么用模块</span><br><span class="line">        如果是内置模块或者是第三方模块直接 improt XXX 调用就好了</span><br><span class="line">        如果是自定义模块</span><br><span class="line">    <span class="keyword">import</span> roo</span><br><span class="line">    I. 引用</span><br><span class="line">    注意<span class="number">1</span>：指名道姓的去引用  例如</span><br><span class="line">    	<span class="built_in">print</span>(roo.变量名)</span><br><span class="line">        <span class="built_in">print</span>(roo.函数名)</span><br><span class="line">    注意<span class="number">2</span>：无论修改与增加，都只会对模块文件造成影响，与调用位置无关</span><br><span class="line">    </span><br><span class="line">    	</span><br><span class="line"><span class="number">4.</span>首次导用函数发生的三件事情</span><br><span class="line">	<span class="keyword">import</span> roo</span><br><span class="line">	I.会制造一个专属roo的名称空间，roo里面的都会在这个名称空间里</span><br><span class="line">    II. 执行 roo.py</span><br><span class="line">    III. 在当前文件创造roo名字，会直接指向模块roo的名称空间</span><br><span class="line"><span class="number">5.</span>如果要导入多个模块，可以以逗号分隔，但是不推荐这么做，推荐分行来导入，例如</span><br><span class="line">	<span class="keyword">import</span> time</span><br><span class="line">    <span class="keyword">import</span> 内置模块</span><br><span class="line">    <span class="keyword">import</span> 第三方   <span class="comment"># 分行导入如果同时有内置 第三方 自定义模块，则按内置 第三方 自定义来先后导入</span></span><br><span class="line">    <span class="keyword">import</span> 第三方</span><br><span class="line">    <span class="keyword">import</span> 自定义</span><br><span class="line">    <span class="keyword">import</span> 自定义</span><br></pre></td></tr></table></figure>

<h2 id="二、一个python文件的两种用途"><a href="#二、一个python文件的两种用途" class="headerlink" title="二、一个python文件的两种用途"></a>二、一个python文件的两种用途</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 当做文件来运行</span><br><span class="line"><span class="number">2.</span> 当做模块来运行</span><br><span class="line"><span class="comment"># 一定要注意名称空间的顺序</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 一个python文件有两种用途</span></span><br><span class="line">    <span class="comment"># 1.被当作程序运行</span></span><br><span class="line">    <span class="comment"># 2.被当作模块导入</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># print(__name__)  # __main__</span></span><br><span class="line"><span class="comment"># 1.当foo.py被运行时，__name__的值为&#x27;__main__&#x27;</span></span><br><span class="line"><span class="comment"># 2.当foo.py被当作模块导入时，__name__的值为&#x27;foo&#x27;</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;文件被执行&#x27;</span>)</span><br><span class="line">    get()</span><br><span class="line">    change()</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="comment"># 被当作模块导入时做的事</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;文件被导入&#x27;</span>)</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 格式：</span></span><br><span class="line"><span class="comment"># m1.py</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f1</span>():</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;xxxxxxxxxxxxxxxxx&#x27;</span>) </span><br><span class="line">    <span class="comment"># f1()</span></span><br><span class="line">    <span class="comment"># if __name__ == &quot;__main__&quot;:</span></span><br><span class="line">    <span class="comment">#     f1()</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># run.py</span></span><br><span class="line">    <span class="keyword">import</span> m1</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">start</span>():</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">        start()</span><br></pre></td></tr></table></figure>

<h2 id="三、from…-import…-的导入模块"><a href="#三、from…-import…-的导入模块" class="headerlink" title="三、from….import….的导入模块"></a>三、from….import….的导入模块</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 模式</span></span><br><span class="line">       <span class="comment"># 1.可以improt语句导入多个模块(推荐使用)</span></span><br><span class="line">       <span class="comment"># import a</span></span><br><span class="line">       <span class="comment"># import b</span></span><br><span class="line">       <span class="comment"># ...</span></span><br><span class="line"></span><br><span class="line">       <span class="comment"># 2.在一行导入，用逗号分隔开不同的模块(不推荐)</span></span><br><span class="line">       <span class="comment"># import a,b,c,...</span></span><br><span class="line">   <span class="comment"># 规范（按以下顺序导入模块，虽然不按顺序也不会报错，但是算是自发的规定）</span></span><br><span class="line">       <span class="comment"># 1.内置模块</span></span><br><span class="line">       <span class="comment"># 2.第三方模块</span></span><br><span class="line">       <span class="comment"># 3.自定义模块</span></span><br><span class="line"></span><br><span class="line">   <span class="comment"># 其他导入语法</span></span><br><span class="line">   <span class="comment"># import foo as f  # 注：这里as f后不要加冒号</span></span><br><span class="line">   <span class="comment"># # 这里相当于把foo换个名字，因为可能会遇到名字特别长的模块名，为了调用时方便，可这样操作</span></span><br><span class="line">   <span class="comment"># f.x  # 等价与foo.x</span></span><br><span class="line">   <span class="comment"># f.get()</span></span><br><span class="line">   <span class="comment"># f.change()</span></span><br><span class="line"></span><br><span class="line">   <span class="comment"># 模块是第一类对象</span></span><br><span class="line"></span><br><span class="line">   <span class="comment"># 自定义模块的命名应该采用纯小写+下划线的风格(python3中)</span></span><br><span class="line">   <span class="comment"># python2中是驼峰体的风格</span></span><br><span class="line"></span><br><span class="line">   <span class="comment"># from 模块 import 模块内的功能</span></span><br><span class="line"></span><br><span class="line">   <span class="keyword">from</span> foo <span class="keyword">import</span> x,get,change <span class="comment">#将模块foo中的x和get导入到当前名称空间</span></span><br><span class="line"></span><br><span class="line">   a=x <span class="comment">#直接使用模块foo中的x赋值给a</span></span><br><span class="line">   get() <span class="comment">#直接执行foo中的get函数</span></span><br><span class="line">   change() <span class="comment">#即便是当前有重名的x，修改的仍然是源文件中的x</span></span><br><span class="line">   <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">   无需加前缀的好处是使得我们的代码更加简洁，</span></span><br><span class="line"><span class="string">   坏处则是容易与当前名称空间中的名字冲突，</span></span><br><span class="line"><span class="string">   如果当前名称空间存在相同的名字，则后定义的名字会覆盖之前定义的名字。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">   &#x27;&#x27;&#x27;</span></span><br><span class="line">   <span class="comment"># *:导入模块中的所有名字</span></span><br><span class="line">   <span class="comment"># from foo import *</span></span><br><span class="line">   <span class="comment"># print(x)</span></span><br><span class="line">   <span class="comment"># print(get)</span></span><br><span class="line">   <span class="comment"># print(change)</span></span><br><span class="line">   <span class="comment"># *也不推荐使用</span></span><br><span class="line">   <span class="comment"># name = &#x27;egon&#x27;</span></span><br><span class="line">   <span class="comment"># from foo import *</span></span><br><span class="line">   <span class="comment"># print(name) # 此时如果*里也有name，则会将egon覆盖掉，但是你只知道导入了所有模块，并不清楚有没有name模块</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="四、互导-循环导入"><a href="#四、互导-循环导入" class="headerlink" title="四、互导(循环导入)"></a>四、互导(循环导入)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 现在有三个py文件 m1.py  m2.py  run.py</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># m1.py文件内容</span></span><br><span class="line">	<span class="keyword">import</span> m2 <span class="keyword">from</span> y</span><br><span class="line">    <span class="built_in">print</span>(y)</span><br><span class="line">    x = <span class="number">111</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># m2.py文件内容</span></span><br><span class="line">	<span class="keyword">import</span> m1 form x</span><br><span class="line">    <span class="built_in">print</span>(x)</span><br><span class="line">    y = <span class="number">222</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># run.py文件内容</span></span><br><span class="line">	<span class="keyword">import</span> m1</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"> 现在执行run.py</span></span><br><span class="line"><span class="string"> 1.首先 import m1 创建了m1的名称空间，执行m1.py，</span></span><br><span class="line"><span class="string"> 2.import m2 from y 注意这里先 import m2创建了m2的名称空间还未执行from y(调用m2中的y)</span></span><br><span class="line"><span class="string"> 3.创建m2的名称空间后，执行m2.py,import m1(已经创建过m1的名称空间)，执行from x 但是m1.py中只执行了import m2，根本</span></span><br><span class="line"><span class="string">	还没有定义x = 111,所以这里找不到x，报错</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 修改方案一：</span></span><br><span class="line">	<span class="comment"># 将x = 111，y = 222提到最上面，执行m1,m2两个py文件时，先将x,y定义好</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 修改方案二：</span></span><br><span class="line">	<span class="comment"># m1.py文件内容</span></span><br><span class="line">    	<span class="function"><span class="keyword">def</span> <span class="title">f1</span>():</span></span><br><span class="line">            <span class="keyword">import</span> m2 <span class="keyword">from</span> y</span><br><span class="line">            <span class="built_in">print</span>(y)</span><br><span class="line">        x = <span class="number">111</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># m2.py文件内容</span></span><br><span class="line">    	<span class="function"><span class="keyword">def</span> <span class="title">f2</span>():</span></span><br><span class="line">            <span class="keyword">import</span> m1 form x</span><br><span class="line">            <span class="built_in">print</span>(x)</span><br><span class="line">        y = <span class="number">222</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># run.py文件内容</span></span><br><span class="line">        <span class="keyword">import</span> m1</span><br><span class="line">        m1.f1()</span><br><span class="line">        </span><br><span class="line">    <span class="comment"># 运用函数定义阶段不执行代码的特点解决这个问题</span></span><br><span class="line">	</span><br></pre></td></tr></table></figure>

<h2 id="五、模块搜索路径"><a href="#五、模块搜索路径" class="headerlink" title="五、模块搜索路径"></a>五、模块搜索路径</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 无论是import还是from...import...在导入模块时都涉及到查找问题</span></span><br><span class="line"><span class="comment"># 模块搜索路径,优先级从高到低</span></span><br><span class="line"><span class="comment"># 1、内存</span></span><br><span class="line"><span class="comment"># 2、内置</span></span><br><span class="line"><span class="comment"># 3、sys.path</span></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="built_in">print</span>(sys.path)</span><br><span class="line"><span class="comment"># 值为一个列表，存放了一系列的文件夹</span></span><br><span class="line"><span class="comment"># 其中第一个文件夹是当前执行文件所在的文件夹</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 例：</span></span><br><span class="line">    <span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">    <span class="keyword">import</span> spam <span class="comment"># 导入模块到内存</span></span><br><span class="line">    <span class="built_in">print</span>(spam.money)</span><br><span class="line">    </span><br><span class="line">    time.sleep(<span class="number">10</span>) <span class="comment"># 在这10秒内将spam.py文件删除</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">import</span> spam <span class="comment"># 10秒后，仍能调用，说明是从内存中找的</span></span><br><span class="line">    <span class="built_in">print</span>(spam.money)</span><br><span class="line">    <span class="comment"># 代码运行结束后可在运行一次，会发现报错找不到spam.py</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 不在同一个文件内，想调用另一个文件内的模块</span></span><br><span class="line">	<span class="comment"># 方法一：</span></span><br><span class="line">         <span class="keyword">import</span> sys</span><br><span class="line">         <span class="built_in">print</span>(sys.path)</span><br><span class="line">         sys.path.append(<span class="string">r&#x27;D:\py18期\day26\代码\aaa&#x27;</span>) <span class="comment"># 想调用文件的模块</span></span><br><span class="line">         <span class="keyword">import</span> spam</span><br><span class="line"> 	<span class="comment"># 方法二：</span></span><br><span class="line">		<span class="keyword">from</span> aaa.bbb.ccc <span class="keyword">import</span> spam  <span class="comment"># 导入语句中出现的点，代表路径分隔符</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># spam.money  # 使用语句中出现的点，代表问某一个名称空间要名字</span></span><br></pre></td></tr></table></figure>



<h2 id="time模块"><a href="#time模块" class="headerlink" title="time模块"></a>time模块</h2><h2 id="datetime模块"><a href="#datetime模块" class="headerlink" title="datetime模块"></a>datetime模块</h2><h2 id="random模块"><a href="#random模块" class="headerlink" title="random模块"></a>random模块</h2><h2 id="os模块"><a href="#os模块" class="headerlink" title="os模块"></a>os模块</h2><h2 id="sys模块"><a href="#sys模块" class="headerlink" title="sys模块"></a>sys模块</h2><h2 id="shutil模块"><a href="#shutil模块" class="headerlink" title="shutil模块"></a>shutil模块</h2><h2 id="struct模块"><a href="#struct模块" class="headerlink" title="struct模块"></a>struct模块</h2><h2 id="socket模块"><a href="#socket模块" class="headerlink" title="socket模块"></a>socket模块</h2><h2 id="序列化与反序列化"><a href="#序列化与反序列化" class="headerlink" title="序列化与反序列化"></a>序列化与反序列化</h2><h2 id="json的使用"><a href="#json的使用" class="headerlink" title="json的使用"></a>json的使用</h2><h2 id="猴子补丁"><a href="#猴子补丁" class="headerlink" title="猴子补丁"></a>猴子补丁</h2><h2 id="pickle的使用"><a href="#pickle的使用" class="headerlink" title="pickle的使用"></a>pickle的使用</h2>
            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/29/%E6%A8%A1%E5%9D%97%E8%B0%83%E7%94%A8/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/29/%E6%A8%A1%E5%9D%97%E8%B0%83%E7%94%A8/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
